package com.superme.UAA.service;

import com.superme.UAA.data.Client;
import com.superme.UAA.data.ClientReposotory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by sm on 2017/5/24.
 */
@Transactional
@Service
public class CustomerClientDetailsService implements ClientDetailsService {


    private final ClientReposotory clientappReposotory;

    @Autowired
    public CustomerClientDetailsService(ClientReposotory clientappReposotory) {
        this.clientappReposotory = clientappReposotory;
    }

    @Override
    public ClientDetails loadClientByClientId(String client_Id) throws ClientRegistrationException {
        Client _client =  this.clientappReposotory.findByClientid(client_Id);
        if (_client == null) {
                throw new ClientRegistrationException(String.format("Client %s does not exist!", client_Id));
        }
        return new ClientRepositoreyClientDetails(_client);

    }
    private final static class ClientRepositoreyClientDetails extends Client implements ClientDetails
    {

        private static final long serialVersionUID = 1L;
        public ClientRepositoreyClientDetails(Client _client)
        {
            super(_client);

        }
        @Override
        public String getClientId() {
            return super.getClient_id();
        }

        @Override



        public Set<String> getResourceIds() {
            Set<String> set = new HashSet<String>();
            if(super.getResource_ids()!=null) {
                List list = Arrays.asList(super.getResource_ids().split(","));
                set.addAll(list);
            }
            return set;
        }

        @Override
        public boolean isSecretRequired() {
            return true;
        }

        @Override
        public String getClientSecret() {
            return super.getClient_secret();
        }

        @Override
        public boolean isScoped() {
            return true;
        }

        @Override
        public Set<String> getAuthorizedGrantTypes() {
            Set<String> set = new HashSet<String>();
            if(super.getAuthorized_grant_types()!=null) {
                List list = Arrays.asList(super.getAuthorized_grant_types().split(","));
                set.addAll(list);
            }
            return set;
        }

        @Override
        public  Collection<GrantedAuthority> getAuthorities()
        {
            Collection<GrantedAuthority> _gaus= new ArrayList<GrantedAuthority>();

            return _gaus;
        }

        @Override

        public Set<String> getRegisteredRedirectUri() {
            return null;
        }

        @Override
        public Integer getAccessTokenValiditySeconds() {
            return super.getAccess_token_validity();
        }

        @Override
        public Integer getRefreshTokenValiditySeconds() {
            return super.getAccess_token_validity();
        }

        @Override
        public boolean isAutoApprove(String scope) {
            super.setAutoapprove(Boolean.getBoolean(scope));
            return super.isAutoapprove();
        }

        @Override
        public Map<String, Object> getAdditionalInformation() {
            Map<String, Object> addinfo = new HashMap<String, Object>();
            if(super.getAdditional_information()!=null)
            {
                addinfo.put("Additional_information",super.getAdditional_information());
            }
            //eturn super.getAdditional_information();
            return addinfo;
        }

        @Override
        public Set<String> getScope(){
            Set<String> set = new HashSet<String>();
            if(super.get_Scope()!=null) {
                List list = Arrays.asList(super.get_Scope().split(","));
                set.addAll(list);
            }
            return set;

        }
    }
}
